﻿using Common.Models;
using Common.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Wallpaper.Models.DTOs.Base;
using Wallpaper.Models.DTOs.Requests.Category;
using Wallpaper.Models.DTOs.Responses.Category;
using Wallpaper.Models.Entities;
using Wallpaper.Models.Options;
using Wallpaper.Services.IService;

namespace WallpaperApi.Controllers
{
    [Route("api/categories")]
    [ApiController]
    [Authorize]
    public class CategoriesController : BaseController
    {

        readonly ICategoryService _categoryService;
        readonly ILogger<CategoriesController> _logger;

        public CategoriesController(ICategoryService categoryService, ILogger<CategoriesController> logger)
        {
            this._categoryService = categoryService;
            this._logger = logger;
        }



        /// <summary>
        /// 壁纸分类创建
        /// </summary>
        /// <param name="request">创建请求</param>
        /// <returns>创建结果</returns>
        [HttpPost]
        [ProducesResponseType(typeof(ApiResponse<Object>), 200)]
        [ProducesResponseType(typeof(ApiResponse<Object>), 400)]
        [Authorize("system:categories:add")]
        public async Task<ApiResponse<Object>> Create([FromBody] CategoryRequest request)
        {
            // 直接调用，让异常被全局异常处理中间件捕获
            var result = await _categoryService.CreateAsync(request);
            return ApiResponse<Object>.Success();
        }



        /// <summary>
        /// 根据id更新
        /// </summary>
        /// <param name="id"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [Authorize("system:categories:edit")]
        public async Task<ApiResponse<Object>> Update(string id,[FromBody] CategoryRequest request)
        {
            // 直接调用，让异常被全局异常处理中间件捕获
            var result = await _categoryService.UpdateAsync(id, request);
            return ApiResponse<Object>.Success();
        }


        /// <summary>
        /// 根据id删除壁纸分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        [Authorize("system:categories:delete")]
        public async Task<ApiResponse<Object>> Delete(string id)
        {
           bool result=await _categoryService.DeleteById(id);
            if (!result)
            {
                return ApiResponse<Object>.Fail("操作失败");
            }
           return ApiResponse<Object>.Success();
        }


        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize("system:categories:query")]
        public async Task<ApiResponse<PagedResult<CategoryResponse>>> Page([FromQuery] QueryRequest request)
        {
            PagedResult<CategoryResponse> result = await _categoryService.PageAsync(request);
            return ApiResponse<PagedResult<CategoryResponse>>.Success(result);
        }


        /// <summary>
        /// 获取下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet("options")]
        public async Task<ApiResponse<List<OptionsResponse>>> Options()
        {
            List<OptionsResponse> optionsResponses = await _categoryService.OptionsAsync();
            return ApiResponse<List<OptionsResponse>>.Success(optionsResponses);
        }


        /// <summary>
        /// 获取小程序分类封面
        /// </summary>
        /// <returns></returns>
        [HttpGet("cover")]
        [AllowAnonymous]
        public async Task<ApiResponse<List<CoverResponse>>> GetCover()
        {
           List<CoverResponse>  coverResponses= await  _categoryService.getCoverAsync();
           return ApiResponse<List<CoverResponse>>.Success(coverResponses);
        }
    }
}
